package com.park.controller;

import com.park.common.bean.CommentInputView;
import com.park.common.bean.PageBean;
import com.park.common.bean.ResponseBean;
import com.park.common.exception.MessageException;
import com.park.common.poo.UserOperator;
import com.park.common.poo.CommentInfo;
import com.park.platform.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("comment")
public class CommentController extends BaseController {

    @Autowired
    private ICommentService commentService;

    @RequestMapping("commentIndex")
    public String toComments(){
        return "comment";
    }

    @RequestMapping("getComments")
    public String getComments(CommentInputView commentInputView, Model model){
        try{
            PageBean pageBean = commentService.getComments(commentInputView);
            model.addAttribute("pageBean", pageBean);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "comment";
    }

    @ResponseBody
    @RequestMapping("saveComment")
    public ResponseBean saveComment(CommentInfo commentInfo){
        try {
            UserOperator userInfo = getUserInfo2();
            if(userInfo == null) throw new MessageException("请您先登录");
            commentInfo.setId(userInfo.getId());
            commentService.newComment(commentInfo);
            return new ResponseBean(true);
        }catch (MessageException e){
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

}
